// pages/commit/commit.js
import {
    legitimateIdCard,
    updateTravelerInfo,
    findTravelerInfos
} from "../../api/home"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        username: "",
        idzhenjian: '',
        phone: '',
        columns: [
            '身份证',
            "护照(Passport)",
            // "台胞证",
            // "军官证",
            // "外国人永居证",
            // "港澳居民来往内地通行证",
        ],
        cardNumber: '',
        cardType: '',
        show: false,
        adultFlag: "",
        faceUrl: "",
        isEdit: false,
        yhListL: "",
        status: "",
        adults: 5,
        options: ''
    },
    //跳转拍照
    gophoto() {
        wx.navigateTo({
            url: '../photo/photo',
        })
    },
    //点击取消按钮
    //返回上级（常用参观者页面）
    buybtn1() {
        wx.navigateBack({
            delta: 1
        });
        //
    },
    //跳转查看人脸图
    cookLook() {
        // console.log("655555");
        wx.navigateTo({
            url: '../Manface/Manface',
        })
    },
    // 打开证件类型选择器
    onOpen() {
        this.setData({
            show: true
        })
    },
    // 关闭证件类型选择器
    onClose() {
        this.setData({
            show: false
        })
    },
    //点击把照片删掉
    xxxx() {
        console.log("5555");
        let _this = this
        wx.showModal({
            title: '确定要删除吗？',
            success(res) {
                if (res.confirm) {
                    _this.setData({
                        faceUrl: ''
                    })
                    wx.setStorageSync('renlianimg', '')
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    // 证件类型选择器确认回调
    onConfirm(e) {
        this.setData({
            cardType: e.detail.value,
            show: false
        })
        console.log(e);
        console.log(this.data.cardType);
    },
    //返回上一级
    onClickLeft() {
        wx.navigateBack({
            delta: 1,
        })
    },
    //点击确认
    buybtn2() {
        if (this.data.username == "") {
            wx.showToast({
                title: '用户名未填写',
                icon: "none"
            })
        } else if (this.data.cardType === "") {
            wx.showToast({
                title: '请选择证件类型',
                icon: "none"
            })
        } else if(this.data.phone != ""){
            const tel = /^((13[0-9])|(14[5-9])|(15([0-3]|[5-9]))|(16[6-7])|(17[1-8])|(18[0-9])|(19[1|3])|(19[5|6])|(19[8|9]))\d{8}$/
            const phone = tel.test(this.data.phone)
            console.log(phone);
            if (phone != true) {
                wx.showToast({
                    title: '电话号有误',
                    icon: "none",
                })
            }else{
                let _this = this
                if (_this.data.options.isflge == "1") {
                    _this.data.yhListL.filter(item => {
                        console.log(item, "888888888888888888");
                    })
                    const otherArr = _this.data.yhListL.filter(item => item.id !== this.data.id)
                    if (otherArr.filter(item => item.cardNum === this.data.idzhenjian).length > 0) {
                        wx.showToast({
                            title: '身份证已经存在',
                            icon: 'none'
                        })
                        return
                    }
                    // if (otherArr == true) {
                    //     wx.showToast({
                    //         title: '身份证号码重复',
                    //         icon: 'none'
                    //     })
                    // } 
                    else {
                        updateTravelerInfo({
                            "adults": _this.data.adultFlag,
                            "cardNum": _this.data.idzhenjian,
                            "certType": _this.data.cardType,
                            "faceUrl": _this.data.faceUrl,
                            "name": _this.data.username,
                            "phone": String(_this.data.phone),
                            id: Number(_this.data.id),
                            status: _this.data.status,
                            flge: this.data.flge
                        }).then(res => {
                            console.log(res, "wishing修改88888888888888888888");
                            wx.navigateBack({
                                delta: 1,
                            })
                        })
                    }
                    console.log(otherArr);
                } else {
                    const otherArr = _this.data.yhListL.filter(item => item.id !== this.data.id)
                    if (otherArr.filter(item => item.cardNum === this.data.idzhenjian).length > 0) {
                        wx.showToast({
                            title: '身份证已经存在',
                            icon: 'none'
                        })
                        return
                    }
                    // if (otherArr == true) {
                    //     wx.showToast({
                    //         title: '身份证号码重复',
                    //         icon: 'none'
                    //     })
                    // } 
                    else {
                        updateTravelerInfo({
                            "adults": _this.data.adultFlag,
                            "cardNum": _this.data.idzhenjian,
                            "certType": _this.data.cardType,
                            "faceUrl": _this.data.faceUrl,
                            "name": _this.data.username,
                            "phone": String(_this.data.phone),
                            id: Number(_this.data.id),
                            status: _this.data.status,
                            flge: this.data.flge
                        }).then(res => {
                            console.log(res, "wishing修改88888888888888888888");
                            wx.navigateBack({
                                delta: 1,
                            })
                        })
                    }
                }
            }
        } else if (this.data.idzhenjian == "") {
            wx.showToast({
                title: '请输入证件号',
                icon: "none"
            })
        } else if (this.data.cardType === "身份证") {
            let _this = this
            legitimateIdCard({
                idCard: _this.data.idzhenjian
            }).then(res => {
                console.log(res);
                _this.setData({
                    //判断用户是否未成年  true成年   false未成年
                    adultFlag: res.data.adultFlag
                })
                //判断身份证号是否有误
                if (res.data.cardNumFlag == false) {
                    wx.showToast({
                        title: '证件号有误',
                        icon: "error"
                    })
                } else{
                    console.log(_this.data.options.isflge);
                    if (_this.data.options.isflge == "1") {
                        _this.data.yhListL.filter(item => {
                            console.log(item, "888888888888888888");
                        })
                        const otherArr = _this.data.yhListL.filter(item => item.id !== this.data.id)
                        if (otherArr.filter(item => item.cardNum === this.data.idzhenjian).length > 0) {
                            wx.showToast({
                                title: '身份证已经存在',
                                icon: 'none'
                            })
                            return
                        }
                        // if (otherArr == true) {
                        //     wx.showToast({
                        //         title: '身份证号码重复',
                        //         icon: 'none'
                        //     })
                        // } 
                        else {
                            updateTravelerInfo({
                                "adults": _this.data.adultFlag,
                                "cardNum": _this.data.idzhenjian,
                                "certType": _this.data.cardType,
                                "faceUrl": _this.data.faceUrl,
                                "name": _this.data.username,
                                "phone": String(_this.data.phone),
                                id: Number(_this.data.id),
                                status: _this.data.status,
                                flge: this.data.flge
                            }).then(res => {
                                console.log(res, "wishing修改88888888888888888888");
                                wx.navigateBack({
                                    delta: 1,
                                })
                            })
                        }
                        console.log(otherArr);
                    } else {
                        const otherArr = _this.data.yhListL.filter(item => item.id !== this.data.id)
                        if (otherArr.filter(item => item.cardNum === this.data.idzhenjian).length > 0) {
                            wx.showToast({
                                title: '身份证已经存在',
                                icon: 'none'
                            })
                            return
                        }
                        // if (otherArr == true) {
                        //     wx.showToast({
                        //         title: '身份证号码重复',
                        //         icon: 'none'
                        //     })
                        // } 
                        else {
                            updateTravelerInfo({
                                "adults": _this.data.adultFlag,
                                "cardNum": _this.data.idzhenjian,
                                "certType": _this.data.cardType,
                                "faceUrl": _this.data.faceUrl,
                                "name": _this.data.username,
                                "phone": String(_this.data.phone),
                                id: Number(_this.data.id),
                                status: _this.data.status,
                                flge: this.data.flge
                            }).then(res => {
                                console.log(res, "wishing修改88888888888888888888");
                                wx.navigateBack({
                                    delta: 1,
                                })
                            })
                        }
                    }


                }


            })
        } else{

            if (this.data.status === "1") {
                updateTravelerInfo({
                    "adults": this.data.adultFlag,
                    "cardNum": this.data.idzhenjian,
                    "certType": this.data.cardType,
                    "faceUrl": this.data.faceUrl,
                    "name": this.data.username,
                    "phone": String(this.data.phone),
                    id: Number(this.data.id),
                    status: this.data.status
                }).then(res => {
                    console.log(res, "wishing修改88888888888888888888");
                    wx.navigateBack({
                        delta: 1,
                    })
                })
            } else {
                updateTravelerInfo({
                    "adults": this.data.adultFlag,
                    "cardNum": this.data.idzhenjian,
                    "certType": this.data.cardType,
                    "faceUrl": this.data.faceUrl,
                    "name": this.data.username,
                    "phone": String(this.data.phone),
                    id: Number(this.data.id)
                }).then(res => {
                    console.log(res, '修改者');
                    if (res.code == 1000) {
                        wx.navigateBack({
                            delta: 1,
                        })
                    } else {
                        wx.showModal({
                            title: "温馨提示",
                            cancelColor: res.data.msg,
                        })
                    }
                })
            }

        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        this.setData({
            options: options,
            username: options.username,
            active: this.data.columns.indexOf(options.certType),
            cardType: options.certType,
            idzhenjian: options.cardNumber,
            faceUrl: options.faceUrl,
            adults: options.adults,
            phone: options.phone,
            id: options.id,
            isflge: options.isflge,
            status: options.status,
            flge: options.flge
        })

        findTravelerInfos().then(res => {
            console.log(res);
            this.setData({
                yhListL: res.data
            })
            console.log(this.data.yhListL);
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (this.data.faceUrl) {
            this.setData({
                faceUrl: wx.setStorageSync('renlianimg', '')
            })
        } else {
            this.setData({
                faceUrl: wx.getStorageSync('renlianimg')
            })
        }


        // if (!this.data.isEdit) {
        //     this.setData({
        //         faceUrl: wx.getStorageSync('renlianimg')
        //     })
        // }
        // else{
        //     wx.setStorageSync('renlianimg', '')
        // }

        // wx.setStorageSync('renlianimg', '')
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})